<?php

namespace addons\yunfood\model;

use addons\yunfood\library\Logic;
use addons\yunfood\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 礼品卡 - 卡密
 */
class ShopDesk extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_shop_desk';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_NORMAL);
    }

    // 自定义初始化
    protected static function init()
    {
        FundUser::event('before_insert', function ($item) {
            // 对象默认值 - 必须使用当前类来初始化
            Logic::setDefaultValue($item, [
            ]);

            return true;
        });
    }

    // 门店数据
    public function shopData()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id');
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('正常'), self::STATUS_HIDDEN => __('禁用')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $statusList = self::getStatusList();

        return $statusList[$data['status']] ?? $data['status'];
    }

    // 桌号状态
    const STATE_EMPTY   = 'empty';
    const STATE_CONFIRM = 'confirm';
    const STATE_ING     = 'ing';

    public static function getStateList()
    {
        return [self::STATE_EMPTY => __('空闲中'), self::STATE_CONFIRM => __('待打印'), self::STATE_ING => __('就餐中')];
    }

    public function getStateAttr($value, $data)
    {
        $state = self::STATE_EMPTY;
        if ($data['last_order_time']) {
            if ($data['last_order_time'] >= time() - 3600 * 2) {
                if ($data['last_order_status']) {
                    $state = self::STATE_ING;
                } else {
                    $state = self::STATE_CONFIRM;
                }
            }
        }

        return $state;
    }

    public function getStateTextAttr($value, $data)
    {
        $stateList = self::getStateList();

        return $stateList[$this->state] ?? $this->state;
    }

    public function getLastOrderTimeTextAttr($value, $data)
    {
        if (empty($data['last_order_time'])) {
            return '从未下单';
        }
        $splitTime = time() - $data['last_order_time'];
        if ($splitTime < 300) {
            $timeStr = '5分钟内下单';
        } elseif ($splitTime < 900) {
            $timeStr = '15分钟内下单';
        } elseif ($splitTime < 1800) {
            $timeStr = '半小时内下单';
        } elseif ($splitTime < 3600) {
            $timeStr = '一小时内下单';
        } elseif ($splitTime < 5600) {
            $timeStr = '一个半小时内下单';
        } elseif ($splitTime < 5600) {
            $timeStr = '两小时内下单';
        } else {
            $timeStr = date('Y-m-d H:i', $data['last_order_time']);
        }

        return $timeStr;
    }

    // 获取select列表
    public static function getOptionList($shopId)
    {
        $options = [];
        $dataList = ShopDesk::field(['id', 'number'])->where('shop_id', $shopId)->select();
        foreach ($dataList as $item) {
            $options[$item['id']] = $item['number'];
        }

        return $options;
    }
}
